package com.zz.parkingsystem.controller.admin;

import com.zz.parkingsystem.dto.PageDTO;
import com.zz.parkingsystem.entity.SystemLogin;
import com.zz.parkingsystem.mapper.SystemLoginMapper;
import com.zz.parkingsystem.service.SystemLoginService;
import com.zz.parkingsystem.util.*;
import com.zz.parkingsystem.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author wangyuxiang
 */
@RestController
@RequestMapping("/adminLog")
@Api(tags = "管理员的登录日志")
public class AdminSystemLoginController {
    @Resource
    private SystemLoginService systemLoginService;

    @Resource
    private SystemLoginMapper systemLoginMapper;

    @PostMapping("/queryAllSystemLogin")
    @ApiOperation(value = "查询所有登录日志")
    @ApiParam(name = "查询所有登录日志", value = "无参数")
    public Result queryAll(@RequestBody PageDTO pageDTO) {
        PageResult list = systemLoginService.queryAllSystemLogin(pageDTO);
        return ResultGenerator.genSuccessResult(list);
    }

    @DeleteMapping("/deleteSL/{id}")
    @ApiOperation(value = "删除一条登录日志")
    @ApiParam(name = "删除一条登录日志", value = "slId")
    public Result del(@PathVariable("id") Integer slId) {
        int i = systemLoginService.deleteByPrimaryKey(slId.longValue());
        return ResultGenerator.genSuccessResult();
    }


    /**
     * 从EXCEL导入到数据库
     */
    @RequestMapping(value = "/importExcel")
    public Result readExcel(MultipartFile file, HttpServletRequest request
    ) throws Exception {
        PageData pd = new PageData();
        if (null != file && !file.isEmpty()) {
            String filePath = request.getSession().getServletContext().getRealPath("/upload");                            //文件上传路径
            String fileName = FileUpload.fileUp(file, filePath, "userexcel");//执行上传
            List<PageData> listPd = (List) ObjectExcelRead.readExcel(filePath, fileName, 1, 0, 0);    //执行读EXCEL操作,读出的数据导入List 2:从第2行开始；0:从第A列开始；0:第0个sheet

            for (int i = 0; i < listPd.size(); i++) {
             //   User user = new User();
                SystemLogin systemLogin = new SystemLogin();
                String slId = listPd.get(i).getString("var0");
                int id = 0;
                if (slId.indexOf(".") != -1) {
                    id = Integer.parseInt(slId.substring(0, slId.indexOf(".")));
                } else {
                    id = Integer.parseInt(slId);
                }
                systemLogin.setSlId(id);
                systemLogin.setSlLoginUser(listPd.get(i).getString("var1"));
                systemLogin.setSlLoginTime(new Date( listPd.get(i).getString("var2")));
                systemLogin.setSlIp(listPd.get(i).getString("var3"));
               systemLoginService.insertSelective(systemLogin);
            }
        }
      return   ResultGenerator.genSuccessResult("/upload/");
    }

    /**
     * 数据导出到excel
     *
     * @return
     */
    @RequestMapping("/excel")
    public ModelAndView exportExcel() {

        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            List<String> titles = new ArrayList<>();
            titles.add("日志编号");
            titles.add("管理员");
            titles.add("登陆时间");
            titles.add("登录IP");
            dataMap.put("titles", titles);

            List<SystemLogin> list = systemLoginMapper.queryAllSystemLogin();
            List<PageData> varList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", list.get(i).getSlId());
                vpd.put("var2", list.get(i).getSlLoginUser());
                vpd.put("var3", list.get(i).getSlLoginTime());
                vpd.put("var4", list.get(i).getSlIp());
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

}
